package com.taiji.dianthus.controller.sms;

import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sms.SmsGroup;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sms.SmsGroupService;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.sms.Sdk2SMSSendService;
import com.taiji.dianthus.util.IPUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_DEL;
import static com.taiji.dianthus.common.Constant.LOG_OPERATION_QUERY;

/**
 * @ClassName SmsDraftsViewController
 * @Description 草稿箱
 * @Author H.M
 * @Date 2020/7/15
 */
@RequestMapping(value = "/sms/drafts")
@Controller
public class SmsDraftsViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private SmsInfoService smsInfoService;

    @Autowired
    private SmsGroupService groupService;


    /**
     * 草稿箱页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String sendMessageIndex(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage, String startDate, String endDate) {
        try {
            currentPage = getCurrentPage(currentPage);
            pageSize = getPageSize(pageSize);
            DSysUser currentUser = currentUser();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            String url = "/sms/drafts/list?pageSize=10";
            Date start = null;
            if (!StringUtils.isEmpty(startDate)) {
                url += "&startDate=" + startDate;
                model.addAttribute("startDate", startDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse(startDate));
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                start = calendar.getTime();
            }
            Date end = null;
            if (!StringUtils.isEmpty(endDate)) {
                url += "&endDate=" + endDate;
                model.addAttribute("endDate", endDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse(endDate));
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                end = calendar.getTime();
            }
            if (currentPage != null) {
                model.addAttribute("currentPage", currentPage + 1);
            }
            BusinessMessage message = smsInfoService.draftsList(currentPage, pageSize, start, end, currentUser.getId(), 2);
            model.addAttribute("url", url);
            model.addAttribute("data", message.getData());
            model.addAttribute("currentUser", currentUser);
            model.addAttribute("menus", listMenu());
            sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                    IPUtil.getIpAddr(request), "查询草稿箱列表", message.success ? 1 : 0, message.getMsg());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "/pages/sms/smsDraftsList";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public BusinessMessage delete(HttpServletRequest request, String id) {
        BusinessMessage message = smsInfoService.deleteDrafts(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "删除草稿", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    /**
     * 草稿箱详情
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/one")
    public String getSmsOne(HttpServletRequest request, Model model, Integer currentPage, String startDate, String endDate, String id) {
        try {
            DSysUser currentUser = currentUser();
            String url = "/sms/drafts/list?pageSize=10";
            if (!StringUtils.isEmpty(startDate)) {
                url += "&startDate=" + startDate;
            }
            if (!StringUtils.isEmpty(endDate)) {
                url += "&endDate=" + endDate;
            }
            if (currentPage != null) {
                url += "&currentPage=" + currentPage;
            }
            SmsInfo info = smsInfoService.getOne(id);
            model.addAttribute("url", url);
            model.addAttribute("data", info);
            model.addAttribute("currentUser", currentUser);
            model.addAttribute("menus", listMenu());
            sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                    IPUtil.getIpAddr(request), "跳转草稿详情页面", 1, "查看草稿详情");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/pages/sms/smsDraftsOne";
    }


    /**
     * 草稿箱发送
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/forward")
    public String forwardSms(HttpServletRequest request, Model model, Integer currentPage, String startDate, String endDate, String id, String flag) {
        String pageUrl = "/pages/sms/forwardSms";
        try {
            DSysUser currentUser = currentUser();
            String url = "/sms/drafts/list?pageSize=10";
            if (!StringUtils.isEmpty(startDate)) {
                url += "&startDate=" + startDate;
            }
            if (!StringUtils.isEmpty(endDate)) {
                url += "&endDate=" + endDate;
            }
            if (currentPage != null) {
                url += "&currentPage=" + currentPage;
            }
            SmsInfo info = smsInfoService.getOne(id);
            if (!StringUtils.isEmpty(flag) && !"".equals(flag) && "drafts".equals(flag)) {//判断转发跳转的是群发页面 还是发送页面
                pageUrl = "/pages/sms/sendGroupMessage";
                info.setSmsGroupId(null);
                List<SmsGroup> groupList = groupService.getAllGroupNoPage(currentUser.getId());
                model.addAttribute("groupList", groupList);
                model.addAttribute("flagStr", flag);
            } else {
                JSONArray treeObj = orgService.getOrgUserTree();
                model.addAttribute("tree", treeObj);
            }
            model.addAttribute("url", url);
            model.addAttribute("data", info);
            model.addAttribute("currentUser", currentUser);
            model.addAttribute("menus", listMenu());
            sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                    IPUtil.getIpAddr(request), "跳转发送页面", 1, "查询草稿数据");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageUrl;
    }

    /**
     * 修改列表定时短信定时发送时间
     *
     * @param id
     * @param msgTime
     * @return
     */
    @ResponseBody
    @RequestMapping("/editTaskTime")
    public BusinessMessage editTaskTime(HttpServletRequest request, String id, String msgTime) {
        BusinessMessage message = new BusinessMessage();
        message = smsInfoService.editTaskTime(id, msgTime);
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request), "修改保存定时发送时间", message.getSuccess() ? 1 : 0, message.getMsg());
        return message;
    }

}
